package com.util;

import java.util.*;
import net.sf.json.JSONArray;

public class TreeBuilder {

	List<Node> nodes = new ArrayList<Node>();

	public TreeBuilder(List<Node> nodes) {
		super();
		this.nodes = nodes;
	}

	/**
	 * 构建JSON树形结构
	 * 
	 * @return
	 */
	public String buildJSONTree() {
		List<Node> nodeTree = buildTree();
		JSONArray jsonArray = JSONArray.fromObject(nodeTree);
		return jsonArray.toString();
	}

	/**
	 * 构建树形结构
	 * 
	 * @return
	 */
	public List<Node> buildTree() {
		List<Node> treeNodes = new ArrayList<Node>();
		List<Node> rootNodes = getRootNodes();
		for (Node rootNode : rootNodes) {
			buildChildNodes(rootNode);
			treeNodes.add(rootNode);
		}
		return treeNodes;
	}

	/**
	 * 递归子节点
	 * 
	 * @param node
	 */
	public void buildChildNodes(Node node) {
		List<Node> children = getChildNodes(node);
		if (!children.isEmpty()) {
			for (Node child : children) {
				buildChildNodes(child);
			}
			node.setNodes(children);
		}
	}

	/**
	 * 获取父节点下所有的子节点
	 * 
	 * @param nodes
	 * @param pnode
	 * @return
	 */
	public List<Node> getChildNodes(Node pnode) {
		List<Node> childNodes = new ArrayList<Node>();
		for(int i=0;i<nodes.size();i++){
			if (String.valueOf(pnode.getFd_BranchID()).equals(nodes.get(i).getFd_ParentBranchID())) {
				childNodes.add(nodes.get(i));
			}
		}
		return childNodes;
	}

	/**
	 * 判断是否为根节点
	 * 
	 * @param nodes
	 * @param inNode
	 * @return
	 */
	public boolean rootNode(Node node) {
		boolean isRootNode = true;
		for (Node n : nodes) {
			if (node.getFd_ParentBranchID().equals(String.valueOf(n.getFd_BranchID()))) {
				isRootNode = false;
				break;
			}
		}
		return isRootNode;
	}

	/**
	 * 获取集合中所有的根节点
	 * 
	 * @param nodes
	 * @return
	 */
	public List<Node> getRootNodes() {
		List<Node> rootNodes = new ArrayList<Node>();
		for (Node n : nodes) {
			if (rootNode(n)) {
				rootNodes.add(n);
			}
		}
		return rootNodes;
	}

	public static class Node {

		private Integer fd_BranchID;
		private String fd_ParentBranchID;
		private String text;
		private Integer fd_leveltype;
		private Integer fd_LoginID;
		private List<Node> nodes;

		public Node() {
		}

		public Node(Integer fd_BranchID, String fd_ParentBranchID, String fd_Branchdesc, Integer fd_leveltype) {
			super();
			this.fd_BranchID = fd_BranchID;
			this.fd_ParentBranchID = fd_ParentBranchID;
			this.text = fd_Branchdesc;
			this.fd_leveltype = fd_leveltype;
		}

		public Integer getFd_BranchID() {
			return fd_BranchID;
		}

		public void setFd_BranchID(Integer fd_BranchID) {
			this.fd_BranchID = fd_BranchID;
		}

		public String getFd_ParentBranchID() {
			return fd_ParentBranchID;
		}

		public void setFd_ParentBranchID(String fd_ParentBranchID) {
			this.fd_ParentBranchID = fd_ParentBranchID;
		}



		public Integer getFd_leveltype() {
			return fd_leveltype;
		}

		public void setFd_leveltype(Integer fd_leveltype) {
			this.fd_leveltype = fd_leveltype;
		}

		public String getText() {
			return text;
		}

		public void setText(String text) {
			this.text = text;
		}

		public Integer getFd_LoginID() {
			return fd_LoginID;
		}

		public void setFd_LoginID(Integer fd_LoginID) {
			this.fd_LoginID = fd_LoginID;
		}

		public List<Node> getNodes() {
			return nodes;
		}

		public void setNodes(List<Node> nodes) {
			this.nodes = nodes;
		}
		
		
	}
}